﻿Public Class ColorTripletArgHandler
    Implements IArgHandler

    Public ReadOnly Property DefaultTypeData As String Implements IArgHandler.DefaultTypeData
        Get
            Return ""
        End Get
    End Property

    Public ReadOnly Property DefaultValue(library As RawLibrary, obj As RawObject, argument As ArgumentDefinition) As String Implements IArgHandler.DefaultValue
        Get
            Return "7:0:1"
        End Get
    End Property

    Public Function GetNavList(library As RawLibrary, obj As RawObject, argument As ArgumentDefinition, value As String) As System.Collections.Generic.IEnumerable(Of NavPoint) Implements IArgHandler.GetNavList
        Return Nothing
    End Function

    Public Function GetValues(obj As RawObject, argument As ArgumentDefinition) As System.Collections.Generic.IEnumerable(Of String) Implements IArgHandler.GetValues
        Return Nothing
    End Function

    Public Function Parse(argParts As System.Collections.Generic.IEnumerator(Of String), arg As ArgumentDefinition) As String Implements IArgHandler.Parse
        Dim ret As String = argParts.Current

        If GetExtras(arg.TypeData).Contains(argParts.Current) Then
            Return ret
        End If

        If argParts.MoveNext Then
            ret &= ":" & argParts.Current
        End If
        If argParts.MoveNext Then
            ret &= ":" & argParts.Current
        End If
        Return ret
    End Function

    Public ReadOnly Property TypeDataFormat As String Implements IArgHandler.TypeDataFormat
        Get
            Return ""
        End Get
    End Property

    Public Function ValidateArgument(library As RawLibrary, obj As RawObject, argument As ArgumentDefinition, value As String) As String Implements IArgHandler.ValidateArgument
        Dim parts() As String = value.Split(":"c)

        If parts.Count = 1 Then
            For Each extra As String In GetExtras(argument.TypeData)
                If parts(0) = extra Then
                    Return Nothing
                End If
            Next
            Return "Color tripplets must be in the form ""[0-15]:[0-15]:[0-1]"""
        End If

        If parts.Count <> 3 Then
            Return "Color tripplets must be in the form ""[0-15]:[0-15]:[0-1]"""
        End If
        Return Nothing
    End Function

    Public Function ValidateTypeData(typeData As String) As Boolean Implements IArgHandler.ValidateTypeData

        If typeData <> "" Then
            Dim parts() As String = typeData.Split(" "c)
            For Each part As String In parts
                If Not part.StartsWith("+") Then
                    Return False
                End If
            Next
        End If

        Return True
    End Function

    Private Function GetExtras(typeData As String) As List(Of String)
        Dim ret As New List(Of String)
        For Each x As String In typeData.Split(" "c)
            If x.StartsWith("+"c) Then ret.Add(x.Substring(1))
        Next
        Return ret
    End Function
End Class
